package com.lanou.moving.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou.moving.R;
import com.lanou.moving.adapter.MVClassifyAdapter;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.MVClassify;
import com.lanou.moving.view.MyGridView;
import com.lanou.moving.volley.StringRequestUtf;
import com.lanou.moving.volley.VolleySingleton;

import java.util.List;

/**
 * Created by 梁树珍 on 15/11/3.
 */
public class MVClassifyFragment extends BaseFragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener, AdapterView.OnItemClickListener, RadioGroup.OnCheckedChangeListener {
    private VolleySingleton volleySingleton;
    private StringRequestUtf request;
    private List<MVClassify.DataEntity> classifies;
    private MVClassifyAdapter adapter;
    private int typeId;
    // 箭头方向
    private Drawable toRight;
    private Drawable toBottom;
    private View view;

    private TextView mv_list_all_tv, mv_list_all, mv_list_tag_zero, mv_list_tag_one, mv_list_tag_two;
    private RadioButton mv_list_hot_rb, mv_list_recent_rb;
    private RadioGroup mv_list_rg;
    private LinearLayout mv_list_linear;
    private RelativeLayout mv_list_relative;

    private MyGridView mv_list_grid_zero, mv_list_grid_one, mv_list_grid_two;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mv_classify, null);
        volleySingleton = VolleySingleton.getInstance(mContext);
        intiView();
        getMVType();
        getFragmentManager().beginTransaction().replace(R.id.mv_list_relative, MVListHotFragment
                .newInstance(URL.URL_MV_TYPE_START, URL.URL_MV_TYPE_END, URL.URL_MV_TYPE_MID, 2,
                        0)).commit();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        intiView();
        getMVType();
    }

    private void intiView(){
        mv_list_all = (TextView) view.findViewById(R.id.mv_list_all);//绿背景全部
        mv_list_all.setOnClickListener(this);
        mv_list_all_tv = (TextView) view.findViewById(R.id.mv_list_all_tv);//全部
        mv_list_all_tv.setOnClickListener(this);
        mv_list_hot_rb = (RadioButton) view.findViewById(R.id.mv_list_hot_rb);//最热
        mv_list_recent_rb = (RadioButton) view.findViewById(R.id.mv_list_recent_rb);//最新
        mv_list_rg = (RadioGroup) view.findViewById(R.id.mv_list_rg);
        mv_list_rg.setOnCheckedChangeListener(this);

        toBottom = getResources().getDrawable(R.mipmap.arrow_to_bottom);
        toBottom.setBounds(0, 0, toBottom.getMinimumWidth(), toBottom.getMinimumHeight());
        toRight = getResources().getDrawable(R.mipmap.arrow_to_right);
        toRight.setBounds(0, 0, toRight.getMinimumWidth(), toRight.getMinimumHeight());

        mv_list_linear = (LinearLayout) view.findViewById(R.id.mv_list_linear);
        mv_list_tag_zero = (TextView) view.findViewById(R.id.mv_list_tag_zero);
        mv_list_tag_one = (TextView) view.findViewById(R.id.mv_list_tag_one);
        mv_list_tag_two = (TextView) view.findViewById(R.id.mv_list_tag_two);

        mv_list_grid_zero = (MyGridView) view.findViewById(R.id.mv_list_grid_zero);
        mv_list_grid_zero.setOnItemClickListener(this);
        mv_list_grid_one = (MyGridView) view.findViewById(R.id.mv_list_grid_one);
        mv_list_grid_one.setOnItemClickListener(this);
        mv_list_grid_two = (MyGridView) view.findViewById(R.id.mv_list_grid_two);
        mv_list_grid_two.setOnItemClickListener(this);

        mv_list_relative = (RelativeLayout) view.findViewById(R.id.mv_list_relative);
    }

    private void getMVType(){
        String url = URL.URL_MV_ALL;
        request = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                MVClassify classify = gson.fromJson(response, MVClassify.class);
                classifies = classify.getData();

                adapter = new MVClassifyAdapter(getActivity(), classifies.get(0).getSubType());
                mv_list_grid_zero.setAdapter(adapter);

                adapter = new MVClassifyAdapter(getActivity(), classifies.get(1).getSubType());
                mv_list_grid_one.setAdapter(adapter);

                adapter = new MVClassifyAdapter(getActivity(), classifies.get(2).getSubType());
                mv_list_grid_two.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.mv_list_all_tv:
                if (mv_list_linear.getVisibility() == View.VISIBLE){
                    mv_list_tag_zero.setText(classifies.get(0).getName());
                    mv_list_tag_one.setText(classifies.get(1).getName());
                    mv_list_tag_two.setText(classifies.get(2).getName());
                    mv_list_linear.setVisibility(View.GONE);
                    mv_list_relative.setVisibility(View.VISIBLE);
                    mv_list_all_tv.setCompoundDrawables(null, null, toRight, null);
                    mv_list_all_tv.setCompoundDrawablePadding(3);
                }else {
                    mv_list_linear.setVisibility(View.VISIBLE);
                    mv_list_relative.setVisibility(View.GONE);
                    mv_list_all_tv.setCompoundDrawables(null, null, toBottom, null);
                    mv_list_all_tv.setCompoundDrawablePadding(3);
                }
                break;
            case R.id.mv_list_all:
                getFragmentManager().beginTransaction().replace(R.id.mv_list_relative, MVListHotFragment
                        .newInstance(URL.URL_MV_TYPE_START, URL.URL_MV_TYPE_END, URL.URL_MV_TYPE_MID, 2,
                                0)).commit();
                mv_list_linear.setVisibility(View.GONE);
                mv_list_relative.setVisibility(View.VISIBLE);
                mv_list_all_tv.setCompoundDrawables(null, null, toRight, null);
                mv_list_all_tv.setCompoundDrawablePadding(3);
                mv_list_all_tv.setText("全部");
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        final MVClassify.DataEntity.SubTypeEntity subTypeEntity = (MVClassify.DataEntity.SubTypeEntity)
                parent.getItemAtPosition(position);
        typeId = subTypeEntity.getId();
        if (mv_list_hot_rb.isChecked() == true) {
            getFragmentManager().beginTransaction().replace(R.id.mv_list_relative, MVListHotFragment
                    .newInstance(URL.URL_MV_TYPE_START, URL.URL_MV_TYPE_END, URL.URL_MV_TYPE_MID,
                            2, typeId))
                    .commit();
        }else {
            getFragmentManager().beginTransaction().replace(R.id.mv_list_relative, MVListHotFragment
                    .newInstance(URL.URL_MV_TYPE_START, URL.URL_MV_TYPE_END, URL.URL_MV_TYPE_MID,
                            1, typeId))
                    .commit();
        }
        mv_list_linear.setVisibility(View.GONE);
        mv_list_relative.setVisibility(View.VISIBLE);
        mv_list_all_tv.setCompoundDrawables(null, null, toRight, null);
        mv_list_all_tv.setCompoundDrawablePadding(3);
        mv_list_all_tv.setText(subTypeEntity.getName());

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        String text = (String) mv_list_hot_rb.getText();
        switch (checkedId){
            case R.id.mv_list_hot_rb:
                if (text.equals("全部")) {
                    getFragmentManager().beginTransaction().replace(R.id.mv_list_relative, MVListHotFragment
                            .newInstance(URL.URL_MV_TYPE_START, URL.URL_MV_TYPE_END, URL.URL_MV_TYPE_MID, 2,
                                    0))
                            .commit();
                }else {
                    getFragmentManager().beginTransaction().replace(R.id.mv_list_relative, MVListHotFragment
                            .newInstance(URL.URL_MV_TYPE_START, URL.URL_MV_TYPE_END, URL.URL_MV_TYPE_MID, 2,
                                    typeId))
                            .commit();
                }
                break;
            case R.id.mv_list_recent_rb:
                if (text.equals("全部")) {
                    getFragmentManager().beginTransaction().replace(R.id.mv_list_relative, MVListHotFragment
                            .newInstance(URL.URL_MV_TYPE_START, URL.URL_MV_TYPE_END, URL.URL_MV_TYPE_MID, 1,
                                    0))
                            .commit();
                }else {
                    getFragmentManager().beginTransaction().replace(R.id.mv_list_relative, MVListHotFragment
                            .newInstance(URL.URL_MV_TYPE_START, URL.URL_MV_TYPE_END, URL.URL_MV_TYPE_MID, 1,
                                    typeId))
                            .commit();
                }
                break;
        }
    }
}
